home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Main.bin / PropertyResourceBundle.java < prev    next >
Text File  |  1998-09-22  |  5KB  |  139 lines

  1. /*
  2.  * @(#)PropertyResourceBundle.java    1.8 97/12/05
  3.  *
  4.  * (C) Copyright Taligent, Inc. 1996 - All Rights Reserved
  5.  * (C) Copyright IBM Corp. 1996 - All Rights Reserved
  6.  *
  7.  * Portions copyright (c) 1996 Sun Microsystems, Inc. All Rights Reserved.
  8.  *
  9.  *   The original version of this source code and documentation is copyrighted
  10.  * and owned by Taligent, Inc., a wholly-owned subsidiary of IBM. These
  11.  * materials are provided under terms of a License Agreement between Taligent
  12.  * and Sun. This technology is protected by multiple US and International
  13.  * patents. This notice and attribution to Taligent may not be removed.
  14.  *   Taligent is a registered trademark of Taligent, Inc.
  15.  *
  16.  * Permission to use, copy, modify, and distribute this software
  17.  * and its documentation for NON-COMMERCIAL purposes and without
  18.  * fee is hereby granted provided that this copyright notice
  19.  * appears in all copies. Please refer to the file "copyright.html"
  20.  * for further important copyright and licensing information.
  21.  *
  22.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  23.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  24.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  25.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  26.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  27.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  28.  *
  29.  */
  30. package java.util;
  31.  
  32. import java.util.Properties;
  33. import java.io.InputStream;
  34. import java.io.IOException;
  35.  
  36. /**
  37.  * <code>PropertyResourceBundle</code> is an abstract subclass of
  38.  * <code>ResourceBundle</code> that manages resources for a locale
  39.  * using a set of static strings from a property file. See
  40.  * <code>ResourceBundle</code> for more information about resource
  41.  * bundles in general.
  42.  *
  43.  * <p>
  44.  * The property file contains the keys that you use in your source code
  45.  * in calls to <code>ResourceBundle.getString</code> and similar methods,
  46.  * and their corresponding values, etc.
  47.  * The name of the property file indicates the resource bundle's family
  48.  * and locale.
  49.  *
  50.  * <p>
  51.  * In the following example, the keys are of the form "s1"... The actual
  52.  * keys are entirely up to your choice, so long as they are the same as
  53.  * the keys you use in your program to retrieve the objects from the bundle.
  54.  * Keys are case-sensitive.
  55.  * <blockquote>
  56.  * <pre>
  57.  * s1=3
  58.  * s2=MeinDisk
  59.  * s3=3 Mar 96
  60.  * s4=Der disk '{1}' a {0} a {2}.
  61.  * s5=0
  62.  * s6=keine Datein
  63.  * s7=1
  64.  * s8=ein Datei
  65.  * s9=2
  66.  * s10={0}|3 Datein
  67.  * s11=Der Format worf ein Exception: {0}
  68.  * s12=ERROR
  69.  * s14=Resulte
  70.  * s13=Dialogue
  71.  * s15=Pattern
  72.  * s16=1,3
  73.  * </pre>
  74.  * </blockquote>
  75.  *
  76.  * @see ResourceBundle
  77.  * @see ListResourceBundle
  78.  */
  79. public class PropertyResourceBundle extends ResourceBundle {
  80.     /**
  81.      * Creates a property resource
  82.      * @param file property file to read from.
  83.      */
  84.     public PropertyResourceBundle (InputStream stream) throws IOException {
  85.         lookup.load(stream);
  86.     }
  87.  
  88.     /**
  89.      * Override of ResourceBundle, same semantics
  90.      */
  91.     public Object handleGetObject(String key) {
  92.         Object obj = lookup.get(key);
  93.         return obj; // once serialization is in place, you can do non-strings
  94.     }
  95.  
  96.     /**
  97.      * Implementation of ResourceBundle.getKeys.
  98.      */
  99.     public Enumeration getKeys() {
  100.         Enumeration result = null;
  101.         if (parent != null) {
  102.             final Enumeration myKeys = lookup.keys();
  103.             final Enumeration parentKeys = parent.getKeys();
  104.  
  105.             result = new Enumeration() {
  106.                 public boolean hasMoreElements() {
  107.                     if (temp == null)
  108.                         nextElement();
  109.                     return temp != null;
  110.                 }
  111.  
  112.                 public Object nextElement() {
  113.                     Object returnVal = temp;
  114.                     if (myKeys.hasMoreElements())
  115.                         temp = myKeys.nextElement();
  116.                     else {
  117.                         temp = null;
  118.                         while (temp == null && parentKeys.hasMoreElements()) {
  119.                             temp = parentKeys.nextElement();
  120.                             if (lookup.containsKey(temp))
  121.                                 temp = null;
  122.                         }
  123.                     }
  124.                     return returnVal;
  125.                 }
  126.  
  127.                 Object temp = null;
  128.             };
  129.         } else {
  130.             result = lookup.keys();
  131.         }
  132.         return result;
  133.     }
  134.  
  135.     // ==================privates====================
  136.  
  137.     private Properties lookup = new Properties();
  138. }
  139.